<?
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
header( 'Content-type: application/javascript');

class Memcache_Mockup
{
	function get() { return false; }
	function set() { }
	function delete() {}
}

// src:
$bannerid = intval($_GET['b']);
$siteid = intval($_GET['s']);

//$memcache = new Memcache();
//$memcache->pconnect("localhost", 11211, 1);
$memcache = new Memcache_Mockup;

include 'config/db.php';
if(!mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) exit;
if(!mysql_select_db(DB_DATABASE)) exit;

// получаем инфу по баннеру
$banner = $memcache->get("remedia_banner_".$bannerid);
if(!$banner)
{
	$res = mysql_query("select * from banners where id=".$bannerid);
	if(!$res) exit;
	$banner = mysql_fetch_assoc($res);
	$memcache->set("remedia_banner_".$bannerid, serialize($banner), 0, 4000);
}
else
{	
	$banner = unserialize($banner);
}

// получаем инфу по связке сайт-баннер
$code = $memcache->get("remedia_code_".$bannerid."_".$siteid);
if(!$code)
{
	$res = mysql_query("select * from codes where bannerid=".$bannerid." and siteid=".$siteid);
	if(!$res) exit;
	$code = mysql_fetch_assoc($res);
	$memcache->set("remedia_code_".$bannerid."_".$siteid, serialize($code), 0, 4000);
}
else
{	
	$code = unserialize($code);
}


$cache_key = 'remedia_show_'.$bannerid.'_'.$siteid;
$v = intval($memcache->get($cache_key));
$memcache->set($cache_key, 0, 0, 50000);
mysql_query("update codes set shows=shows+{$v}, clicks=clicks+1 where id=".$code['id']);


header('Location: '.$banner['url']);
exit;